package com.sh.data.engine.web.webapi.controller.datadev;

import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.dto.datadev.*;
import com.sh.data.engine.application.model.dto.workspace.UserResponse;
import com.sh.data.engine.application.model.request.datadev.OfflineDevOperationJobSearchRequest;
import com.sh.data.engine.application.model.request.datadev.OfflineDevTaskManagerSearchRequest;
import com.sh.data.engine.application.service.datadev.OfflineDevOperationV2ApplicationService;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.annotation.userlog.EngineOperation;
import com.sh.data.engine.domain.datadev.offline.service.DatadevJobService;
import com.sh.data.engine.domain.datadev.offline.service.DatadevMenuNodeService;
import com.sh.data.engine.domain.workspace.manager.service.UserService;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Validated
@RestController
@RequestMapping("/offlineDev/operation/v2")
@Tag(name = "数据开发-离线计算-任务运维-V2", description = "离线开发相关API-运维中心-V2")
@Slf4j
public class OfflineDevOperationV2Controller {

    @Autowired
    private OfflineDevOperationV2ApplicationService offlineDevOperationAppService;

    @Autowired
    private DatadevJobService datadevJobService;
    @Autowired
    private DatadevMenuNodeService datadevMenuNodeService;
    @Autowired
    private UserService userService;

    @Operation(summary = "获取任务的统计数-head")
    @GetMapping("/summary")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<OfflineDevOpeartionSummaryResponse> getOfflineDevSummay(
        @Parameter(description = "nodeId 历史任务中使用") @RequestParam(required = false) Long nodeId,
        @Parameter(description = "execType", required = false)
        @RequestParam(value = "execType", required = false)
        Integer execType) {
        var result = offlineDevOperationAppService.getOfflineDevSummary(nodeId, execType);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "今日任务完成情况")
    @GetMapping("/finished/today")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<OfflineDevTodayFinishedResponse> getTodayFinishedResponse() {
        var result = offlineDevOperationAppService.getTodayFinishedResponse();
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "失败任务top 10")
    @GetMapping("/failedTask/top")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<List<OfflineDevFailedTopResponse>> getFailedTaskTopResponse(
        @Parameter(description = "0-三天 1-一周 2- 一个月 3-一天", required = false)
        @RequestParam(value = "timeType", required = true, defaultValue = "1")
        Integer timeType) {
        var result = offlineDevOperationAppService.getFailedTaskTopResponse(timeType);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "运行时长排行榜")
    @GetMapping("/runningTime/top")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<List<OfflineDevRunningTimeTopResponse>> getRunningTimeTopResponse(
        @Parameter(description = "指定时间")
        @RequestParam(required = false)
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        Date runDate) {
        var result = offlineDevOperationAppService.getRunningTimeTopResponse(runDate);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "任务监控异常情况")
    @GetMapping("/taskMonitor/list")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<OfflineDevTaskMonitorResponse> getTaskMonitorListResponse() {
        var result = offlineDevOperationAppService.getTaskMonitorListResponse();
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "异常任务监控")
    @GetMapping("/taskMonitor/error")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<PagerResponse<OfflineDevTaskMonitorErrorTaskResponse>>
    getTaskMonitorErrorTaskResponse(
        @Parameter(description = "pageNum")
        @RequestParam(value = "pageNum", required = false, defaultValue = "1")
        Integer pageNum,
        @Parameter(description = "pageSize")
        @RequestParam(value = "pageSize", required = false, defaultValue = "10")
        Integer pageSize,
        @Parameter(description = "1-三天 2-一周 3- 一个月", required = false)
        @RequestParam(value = "timeCase", required = true, defaultValue = "1")
        Integer timeCase) {
        var result = offlineDevOperationAppService.getTaskMonitorErrorTaskResponse(timeCase, pageNum, pageSize);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "任务管理")
    @GetMapping("/taskManager/page")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<PagerResponse<OfflineDevTaskManageResponse>> getOfflineDevTaskManagePage(
        OfflineDevTaskManagerSearchRequest request) {
        var result = offlineDevOperationAppService.getOfflineDevTaskManagePage(request);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "子任务")
    @GetMapping("/taskManager/children")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<List<OfflineDevTaskManageResponse>> getOfflineDevTaskManageChildren(
        @Parameter(description = "nodeId", required = true) @RequestParam Long nodeId,
        @Parameter(description = "nodeType", required = false) @RequestParam(required = false) Integer nodeType) {
        var result = offlineDevOperationAppService.getOfflineDevTaskManageChildren(nodeId, nodeType);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "查询子任务 ")
    @GetMapping("/job/children")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<List<OfflineDevOperationJobResponse>> getOfflineDevOperationJobList(
        @Parameter(description = "执行记录jobId", required = true) @RequestParam Long jobId,
        @Parameter(description = "nodeType", required = false) @RequestParam(required = false) Integer nodeType) {
        var result = offlineDevOperationAppService.getOfflineDevOperationJobList(jobId, nodeType);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "上线 or 下线")
    @GetMapping("/taskflows/change")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 1)
    public RestResponseEntity<Boolean> saveTaskflowsStatus(
        @Parameter(description = "0-上线 1下线", required = true)
        @RequestParam(value = "isLocked", required = true)
        Integer isLocked,
        @Parameter(description = "node id 以逗号分割", required = true) String nodeIds) {
        var result = offlineDevOperationAppService.saveTaskflowsStatus(isLocked, nodeIds);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "忽略该记录")
    @GetMapping("/ignore")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 1)
    public RestResponseEntity<Boolean> ignoreDataDevJob(String ids) {
        var result = offlineDevOperationAppService.ignoreDataDevJob(ids);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "查询任务列表")
    @GetMapping("/jobList")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<PagerResponse<OfflineDevOperationJobResponse>>
    getOfflineDevOperationJobPage(OfflineDevOperationJobSearchRequest searchRequest) {
        var result = offlineDevOperationAppService.getOfflineDevOperationJobPage(searchRequest);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "查询用户信息列表")
    @GetMapping("/userList")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<List<UserResponse>> getUserList() {
        var result = offlineDevOperationAppService.getUserList();
        return RestResponseEntity.success(result);
    }

    @GetMapping("/task/log")
    @Operation(summary = "查询日志")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<OfflineDevTaskLogResponse> taskLog(
        @Parameter(description = "任务id", required = true) @RequestParam("jobId") Long jobId,
        @Parameter(description = "偏移量", required = true) @RequestParam("offset") Integer offset,
        @Parameter(description = "") @RequestParam(value = "rsHasShow", required = false) String rsHasShow) {
        var result = offlineDevOperationAppService.taskLog(jobId, offset, rsHasShow);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/task/restart")
    @Operation(summary = "任务运行记录：启动/重跑任务")
    @LogExecution(operation = EngineOperation.START_TASK, business = Business.离线开发任务)
//    @Log(
//        business = Business.离线开发任务,
//        operation = Operation.START_TASK)
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 1)
    public RestResponseEntity<Long> taskRestart(
        @Parameter(description = "jobId", required = true) @RequestParam("id") Long id) {
        var result = offlineDevOperationAppService.taskRestart(id);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/task/stop")
    @Operation(summary = "停止任务/批量停止任务")
    @LogExecution(operation = EngineOperation.STOP_TASK_LIST, business = Business.离线开发任务)
//    @Log(
//        business = Business.离线开发任务,
//        operation = Operation.STOP_TASK_LIST)
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 1)
    public RestResponseEntity<Boolean> taskStop(
        @Parameter(description = "job id 以逗号分开", required = true) @RequestParam("logIds") String logIds) {
        var result = offlineDevOperationAppService.taskStop(logIds);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/flow/task/start")
    @Operation(summary = "任务管理：启动、重跑任务")
    @LogExecution(operation = EngineOperation.START_TASK, business = Business.离线开发elt工作流)
//    @Log(
//        business = Business.离线开发elt工作流,
//        operation = Operation.START_TASK)
//    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 1)
    public RestResponseEntity<Long> flowTaskStart(
        @Parameter(description = "node id ", required = true) @RequestParam("nodeId") Long nodeId) {
        var result = offlineDevOperationAppService.flowTaskStart(nodeId);
        return RestResponseEntity.success(result);
    }
}
